package com.ruoyi.pos.service.impl.goods;

import com.ruoyi.pos.domain.goods.PosGoods;
import com.ruoyi.pos.domain.dto.goods.request.GoodsQueryRequest;
import com.ruoyi.pos.domain.dto.goods.response.GoodsResponse;
import com.ruoyi.pos.domain.dto.common.response.BasePageResponse;
import com.ruoyi.pos.mapper.goods.PosGoodsMapper;
import com.ruoyi.pos.service.goods.IPosGoodsService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * POS商品Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-30
 */
@Service
@Transactional(transactionManager = "goodsTransactionManager")
public class PosGoodsServiceImpl implements IPosGoodsService {

    @Autowired
    private PosGoodsMapper posGoodsMapper;

    /**
     * 查询单个商品信息
     * 支持根据ID、编码、条码查询
     * 
     * @param request 查询请求
     * @return 商品信息
     */
    @Override
    public GoodsResponse getGoods(GoodsQueryRequest request) {
        PosGoods goods = null;
        
        if (request.getGoodsId() != null) {
            goods = posGoodsMapper.selectPosGoodsById(request.getGoodsId());
        } else if (StringUtils.isNotEmpty(request.getGoodsCode())) {
            goods = posGoodsMapper.selectPosGoodsByCode(request.getGoodsCode());
        } else if (StringUtils.isNotEmpty(request.getBarcode())) {
            goods = posGoodsMapper.selectPosGoodsByBarcode(request.getBarcode());
        }
        
        if (goods == null) {
            return null;
        }
        
        return convertToResponse(goods);
    }

    /**
     * 分页查询商品列表
     * 
     * @param request 查询请求
     * @return 分页响应
     */
    @Override
    public BasePageResponse<GoodsResponse> queryGoods(GoodsQueryRequest request) {
        PosGoods goods = new PosGoods();
        goods.setGoodsId(request.getGoodsId());
        goods.setGoodsCode(request.getGoodsCode());
        goods.setGoodsName(request.getGoodsName());
        goods.setBarcode(request.getBarcode());
        goods.setCategoryCode(request.getCategoryId() != null ? String.valueOf(request.getCategoryId()) : null);
        goods.setBrandCode(request.getBrandId() != null ? String.valueOf(request.getBrandId()) : null);
        goods.setStatus(request.getStatus());
        
        List<PosGoods> goodsList = posGoodsMapper.selectPosGoodsList(goods);
        List<GoodsResponse> responses = goodsList.stream()
                .map(this::convertToResponse)
                .collect(Collectors.toList());
        
        return new BasePageResponse<>(responses, goodsList.size());
    }

    /**
     * 转换实体为响应DTO
     * 
     * @param goods 商品实体
     * @return 商品响应DTO
     */
    private GoodsResponse convertToResponse(PosGoods goods) {
        GoodsResponse response = new GoodsResponse();
        BeanUtils.copyBeanProp(response, goods);
        return response;
    }
}
